<html>
    <title>Purchase Reports</title>
    <body>

        <%@ page errorPage="error.jsp" %>
        <%@ page import="net.sf.jasperreports.engine.*" %>
        <%@ page import="com.myapp.struts.reports.*" %>
        <%@ page import="net.sf.jasperreports.engine.util.*" %>
        <%@ page import="net.sf.jasperreports.engine.export.*" %>
        <%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
        <%@ page import="net.sf.jasperreports.engine.util.JRProperties" %>
        <%@ page import="java.util.*" %>
        <%@ page import="java.io.*" %>
        <%@ page import="java.sql.* , net.sf.jasperreports.engine.base.*,com.myapp.struts.dao.*,java.math.*"%>
        <%
        request.getSession(true);
        String r = session.getAttribute("rd").toString();
                System.out.println("r-->"+r);
        String v = session.getAttribute("vd").toString();
                System.out.println("v-->"+v);
        String un = session.getAttribute("name").toString();
                System.out.println("un-->"+un);
        String loc = session.getAttribute("location").toString();
                System.out.println("location-->"+loc);
        ResultSet res = null, r1 = null, r2 = null, r3 = null, r4 = null;
        int data = 0;
        Vector v1 = new Vector();
        Vector v2 = new Vector();
        Vector v3 = new Vector();
        Vector v4 = new Vector();
        Vector v5 = new Vector();
        Vector v6 = new Vector();
        Vector v7 = new Vector();
        Vector v8 = new Vector();
        Vector sname = new Vector();
        Connection con = null;
        Connect drv;
        PreparedStatement pstmt;
        drv = new Connect();
        con = drv.mySql();
        if (con != null) {
            File reportFile = new File(application.getRealPath("jrxml/PurchaseReport.jasper"));

            if (!reportFile.exists()) {
                throw new JRRuntimeException("File PurchaseReport.jrxml not found. The report design must be compiled first.");
            }
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
            Map parameters = new HashMap();
            parameters.put("ReportTitle", "Address Report");
            parameters.put("BaseDir", reportFile.getParentFile());

            String sqa = "select count(purchase.iname) from purchase,items where items.itemname=purchase.iname and sitename='" + loc + "' and pdate >='" + r + "' and pdate<='" + v + "'";
            System.out.println(sqa);
            pstmt = con.prepareStatement(sqa);
            r1 = pstmt.executeQuery();
            if (r1.next() == true) {
                data = data + (int) r1.getInt(1);
                System.out.println("entered into r.next===" + data);
            }
            pstmt.close();
            r1.close();
            //------------------------------------
            String sql = "SELECT purchase.iname,sname,purchase.qty,purchase.rate,purchase.value,pdate,purchase.sitename,items.uom" + " FROM purchase,items where items.itemname=purchase.iname and purchase.sitename='" + loc + "' and pdate >= '" + r + "' and pdate<='" + v + "' order by sname,purchase.iname";
            System.out.println(sql);
            pstmt = con.prepareStatement(sql);
            res = pstmt.executeQuery();

            String ena = "", client = "", location = "";
            double qty = 0.0;
            java.util.Date ncdate = null;
            double rate = 0.0, value1 = 0.0;
            while (res.next() == true) {
                ena = (String) res.getString(1);
                client = (String) res.getString(2);
                qty = (double) res.getInt(3);
                rate = (double) res.getDouble(4);
                value1 = (double) res.getDouble(5);
                ncdate = (java.util.Date) res.getDate(6);
                location = (String) res.getString(7);
                v1.add(ena);
                v2.add(client);
                v3.add(qty);
                v4.add(rate);
                v5.add(value1);
                v6.add(ncdate);
                v7.add(location);
                v8.add(res.getString(8));
            }
            //------------------------------------
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
                    new PurchaseRepLocWise(data, v1, v2, v3, v4, v5, v6, v7, v8));


            JRHtmlExporter exporter = new JRHtmlExporter();
            StringBuffer sbuffer = new StringBuffer();
            session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            File destFile = new File(reportFile.getParent(), jasperPrint.getName() + ".html");
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, destFile);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../../../../jrxml/PurchaseReport.html_files/");
            exporter.exportReport();
            con.close();

        }

        %>
        <jsp:forward page="../../../../jrxml/PurchaseReport.html"/>
    </body>
</html>